package br.com.vagacerta.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import br.com.vagacerta.modelo.Cancela;


@Repository
public class CancelaDAO {

	private Connection connection;

	@Autowired
	public CancelaDAO(DataSource dataSource) {
		try {
			this.connection = dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public Cancela retornaCancelaPorId(long idCancela) {
		try {

			String sql = "SELECT * FROM CANCELA WHERE COD_CANCELA = ? LIMIT 1";

			PreparedStatement stmt;
			stmt = connection.prepareStatement(sql);

			stmt.setLong(1, idCancela);
			ResultSet rs = stmt.executeQuery();
			Cancela cancela = new Cancela();

			while (rs.next()) {

				cancela.setCodigoCancela(rs.getLong("COD_CANCELA"));
				cancela.setStatusCancela(rs.getShort("STATUS_CANCELA"));
				cancela.setNomeCancela(rs.getString("NOME_CANCELA"));

			}

			rs.close();
			stmt.close();

			return cancela;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
